package com.slwish.xty.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.slwish.common.base.PresenterFragment;
import com.slwish.common.config.Contants;
import com.slwish.common.utils.SpUtils;
import com.slwish.common.utils.StringUtils;
import com.slwish.xty.R;
import com.slwish.xty.interf.OnFragmentInteractionListener;
import com.slwish.xty.mvp.contract.SearchListHistoryContract;
import com.slwish.xty.mvp.presenter.SearchListHistoryPresenter;
import com.slwish.xty.ui.activity.SearchListActivity;
import com.slwish.xty.widget.flowlayout.FlowLayout;
import com.slwish.xty.widget.flowlayout.TagAdapter;
import com.slwish.xty.widget.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 */
public class SearchListHistoryFragment extends PresenterFragment<SearchListHistoryContract.Presenter> implements SearchListHistoryContract.View {

    @BindView(R.id.flowlayout1)
    TagFlowLayout hotFlowLayout;
    @BindView(R.id.flowlayout2)
    TagFlowLayout historyFlowLayout;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private long userId;

    public SearchListHistoryFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SearchListHistoryFragment.
     */
    public static SearchListHistoryFragment newInstance(String param1, String param2) {
        SearchListHistoryFragment fragment = new SearchListHistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.frag_search_list_history;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mPresenter.queryKeyWord();
        userId = ((SearchListActivity) getActivity()).getUsetId();

        refreshHistory();
    }

    @Override
    protected void initData() {
        super.initData();
        //热门
        hotFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent, Object S) {
//                showError(S.toString());
                if (mListener != null)
                    onButtonPressed(S.toString());
                return true;
            }
        });
        //历史
        historyFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent, Object S) {
                if (mListener != null)
                    onButtonPressed(S.toString());
                return true;
            }
        });
    }

    public void onButtonPressed(String messge) {
        if (mListener != null) {
            mListener.onFragmentInteraction(messge);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    protected SearchListHistoryContract.Presenter initPresenter() {
        return new SearchListHistoryPresenter(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void queryKeyWordSuccess(List<String> list) {

        hotFlowLayout.setAdapter(new TagAdapter<String>(list) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.view_search_hot_tabel_items_text,
                        parent, false);
                view.setText(s);
                return view;
            }
        });
    }

    @Override
    public void queryKeyWordFailed() {

    }
public void refreshHistory(){
    String historyLabel = (String) SpUtils.get(getContext(), Contants.HISTORY_LABEL, "");
//        List<HistorySearchModel> historySearchModelList = HistorySearchDb.query(userId + "");
    if (!StringUtils.isEmpty(historyLabel)) {
        String endString = historyLabel.substring(1);
        String[] split = endString.split(",");
        List<String> historyList = new ArrayList<>();
        for (String string : split) {
            historyList.add(string);
        }
        historyFlowLayout.setAdapter(new TagAdapter<String>(historyList) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.view_search_hot_tabel_items_text,
                        parent, false);
                view.setText(s);
                return view;
            }
        });
    }
}
    @OnClick(R.id.iv_delete)
    public void setOnClick(View view) {
//        HistorySearchDb.delete(userId);
        SpUtils.remove(getContext(),Contants.HISTORY_LABEL);
        historyFlowLayout.onRemove();
    }
}
